Data Visualizations

October 30, 2025

Jo Hardin - AWM + GEM

Visualizing the data

What and why

  • Data visualization is the process of transforming raw data into visual formats, like charts, graphs, or maps, to make it easier to understand and interpret

  • Communicate results clearly and effectively

  • Supports evidence-based decisions across many fields.

What is ggplot2?

  • ggplot2 is an R package for data visualization
  • Encoded functions for numerous different plot types
  • Builds plots in layers using +

Basic structure of a ggplot

We will go over each function one at a time.

# General structure
data |>
  ggplot(aes(x = ..., y = ...)) +
  geom_*() +
  labs("label for you plot and axes")

Births in 2015

library(mosaic)
Births2015
          date births wday year month day_of_year day_of_month day_of_week
1   2015-01-01   8068  Thu 2015     1           1            1           5
2   2015-01-02  10850  Fri 2015     1           2            2           6
3   2015-01-03   8328  Sat 2015     1           3            3           7
4   2015-01-04   7065  Sun 2015     1           4            4           1
5   2015-01-05  11892  Mon 2015     1           5            5           2
6   2015-01-06  12425  Tue 2015     1           6            6           3
7   2015-01-07  12141  Wed 2015     1           7            7           4
8   2015-01-08  12094  Thu 2015     1           8            8           5
9   2015-01-09  11868  Fri 2015     1           9            9           6
10  2015-01-10   8014  Sat 2015     1          10           10           7
11  2015-01-11   7172  Sun 2015     1          11           11           1
12  2015-01-12  11479  Mon 2015     1          12           12           2
13  2015-01-13  11924  Tue 2015     1          13           13           3
14  2015-01-14  12013  Wed 2015     1          14           14           4
15  2015-01-15  12339  Thu 2015     1          15           15           5
16  2015-01-16  11861  Fri 2015     1          16           16           6
17  2015-01-17   8280  Sat 2015     1          17           17           7
18  2015-01-18   7195  Sun 2015     1          18           18           1
19  2015-01-19  10602  Mon 2015     1          19           19           2
20  2015-01-20  12242  Tue 2015     1          20           20           3
21  2015-01-21  12086  Wed 2015     1          21           21           4
22  2015-01-22  11949  Thu 2015     1          22           22           5
23  2015-01-23  11910  Fri 2015     1          23           23           6
24  2015-01-24   8235  Sat 2015     1          24           24           7
25  2015-01-25   7186  Sun 2015     1          25           25           1
26  2015-01-26  11728  Mon 2015     1          26           26           2
27  2015-01-27  11882  Tue 2015     1          27           27           3
28  2015-01-28  11602  Wed 2015     1          28           28           4
29  2015-01-29  11967  Thu 2015     1          29           29           5
30  2015-01-30  11571  Fri 2015     1          30           30           6
31  2015-01-31   7987  Sat 2015     1          31           31           7
32  2015-02-01   7139  Sun 2015     2          32            1           1
33  2015-02-02  11480  Mon 2015     2          33            2           2
34  2015-02-03  12079  Tue 2015     2          34            3           3
35  2015-02-04  11924  Wed 2015     2          35            4           4
36  2015-02-05  11821  Thu 2015     2          36            5           5
37  2015-02-06  11656  Fri 2015     2          37            6           6
38  2015-02-07   8285  Sat 2015     2          38            7           7
39  2015-02-08   7265  Sun 2015     2          39            8           1
40  2015-02-09  11937  Mon 2015     2          40            9           2
41  2015-02-10  12291  Tue 2015     2          41           10           3
42  2015-02-11  11911  Wed 2015     2          42           11           4
43  2015-02-12  12285  Thu 2015     2          43           12           5
44  2015-02-13  11321  Fri 2015     2          44           13           6
45  2015-02-14   8390  Sat 2015     2          45           14           7
46  2015-02-15   7508  Sun 2015     2          46           15           1
47  2015-02-16  11038  Mon 2015     2          47           16           2
48  2015-02-17  11952  Tue 2015     2          48           17           3
49  2015-02-18  12061  Wed 2015     2          49           18           4
50  2015-02-19  11968  Thu 2015     2          50           19           5
51  2015-02-20  11864  Fri 2015     2          51           20           6
52  2015-02-21   8343  Sat 2015     2          52           21           7
53  2015-02-22   7254  Sun 2015     2          53           22           1
54  2015-02-23  11260  Mon 2015     2          54           23           2
55  2015-02-24  11941  Tue 2015     2          55           24           3
56  2015-02-25  11828  Wed 2015     2          56           25           4
57  2015-02-26  11672  Thu 2015     2          57           26           5
58  2015-02-27  11596  Fri 2015     2          58           27           6
59  2015-02-28   7989  Sat 2015     2          59           28           7
60  2015-03-01   7273  Sun 2015     3          60            1           1
61  2015-03-02  11609  Mon 2015     3          61            2           2
62  2015-03-03  12402  Tue 2015     3          62            3           3
63  2015-03-04  11927  Wed 2015     3          63            4           4
64  2015-03-05  11681  Thu 2015     3          64            5           5
65  2015-03-06  11612  Fri 2015     3          65            6           6
66  2015-03-07   7980  Sat 2015     3          66            7           7
67  2015-03-08   6929  Sun 2015     3          67            8           1
68  2015-03-09  11678  Mon 2015     3          68            9           2
69  2015-03-10  12282  Tue 2015     3          69           10           3
70  2015-03-11  11850  Wed 2015     3          70           11           4
71  2015-03-12  12042  Thu 2015     3          71           12           5
72  2015-03-13  10940  Fri 2015     3          72           13           6
73  2015-03-14   8217  Sat 2015     3          73           14           7
74  2015-03-15   7202  Sun 2015     3          74           15           1
75  2015-03-16  11537  Mon 2015     3          75           16           2
76  2015-03-17  12312  Tue 2015     3          76           17           3
77  2015-03-18  11807  Wed 2015     3          77           18           4
78  2015-03-19  11721  Thu 2015     3          78           19           5
79  2015-03-20  12040  Fri 2015     3          79           20           6
80  2015-03-21   8080  Sat 2015     3          80           21           7
81  2015-03-22   7171  Sun 2015     3          81           22           1
82  2015-03-23  11674  Mon 2015     3          82           23           2
83  2015-03-24  12333  Tue 2015     3          83           24           3
84  2015-03-25  11872  Wed 2015     3          84           25           4
85  2015-03-26  12065  Thu 2015     3          85           26           5
86  2015-03-27  11629  Fri 2015     3          86           27           6
87  2015-03-28   8071  Sat 2015     3          87           28           7
88  2015-03-29   7125  Sun 2015     3          88           29           1
89  2015-03-30  11500  Mon 2015     3          89           30           2
90  2015-03-31  12362  Tue 2015     3          90           31           3
91  2015-04-01  11596  Wed 2015     4          91            1           4
92  2015-04-02  12296  Thu 2015     4          92            2           5
93  2015-04-03  11234  Fri 2015     4          93            3           6
94  2015-04-04   7981  Sat 2015     4          94            4           7
95  2015-04-05   6821  Sun 2015     4          95            5           1
96  2015-04-06  11455  Mon 2015     4          96            6           2
97  2015-04-07  12334  Tue 2015     4          97            7           3
98  2015-04-08  11976  Wed 2015     4          98            8           4
99  2015-04-09  11874  Thu 2015     4          99            9           5
100 2015-04-10  11724  Fri 2015     4         100           10           6
101 2015-04-11   8092  Sat 2015     4         101           11           7
102 2015-04-12   7141  Sun 2015     4         102           12           1
103 2015-04-13  11503  Mon 2015     4         103           13           2
104 2015-04-14  12309  Tue 2015     4         104           14           3
105 2015-04-15  11794  Wed 2015     4         105           15           4
106 2015-04-16  11947  Thu 2015     4         106           16           5
107 2015-04-17  11718  Fri 2015     4         107           17           6
108 2015-04-18   8028  Sat 2015     4         108           18           7
109 2015-04-19   7180  Sun 2015     4         109           19           1
110 2015-04-20  11513  Mon 2015     4         110           20           2
111 2015-04-21  12173  Tue 2015     4         111           21           3
112 2015-04-22  11917  Wed 2015     4         112           22           4
113 2015-04-23  11877  Thu 2015     4         113           23           5
114 2015-04-24  11749  Fri 2015     4         114           24           6
115 2015-04-25   8101  Sat 2015     4         115           25           7
116 2015-04-26   7178  Sun 2015     4         116           26           1
117 2015-04-27  11494  Mon 2015     4         117           27           2
118 2015-04-28  12221  Tue 2015     4         118           28           3
119 2015-04-29  11775  Wed 2015     4         119           29           4
120 2015-04-30  11831  Thu 2015     4         120           30           5
121 2015-05-01  12178  Fri 2015     5         121            1           6
122 2015-05-02   8075  Sat 2015     5         122            2           7
123 2015-05-03   7195  Sun 2015     5         123            3           1
124 2015-05-04  11724  Mon 2015     5         124            4           2
125 2015-05-05  12618  Tue 2015     5         125            5           3
126 2015-05-06  11924  Wed 2015     5         126            6           4
127 2015-05-07  11649  Thu 2015     5         127            7           5
128 2015-05-08  11810  Fri 2015     5         128            8           6
129 2015-05-09   8216  Sat 2015     5         129            9           7
130 2015-05-10   7246  Sun 2015     5         130           10           1
131 2015-05-11  11549  Mon 2015     5         131           11           2
132 2015-05-12  12400  Tue 2015     5         132           12           3
133 2015-05-13  11572  Wed 2015     5         133           13           4
134 2015-05-14  11793  Thu 2015     5         134           14           5
135 2015-05-15  12306  Fri 2015     5         135           15           6
136 2015-05-16   8337  Sat 2015     5         136           16           7
137 2015-05-17   7354  Sun 2015     5         137           17           1
138 2015-05-18  11708  Mon 2015     5         138           18           2
139 2015-05-19  12469  Tue 2015     5         139           19           3
140 2015-05-20  12352  Wed 2015     5         140           20           4
141 2015-05-21  12246  Thu 2015     5         141           21           5
142 2015-05-22  12147  Fri 2015     5         142           22           6
143 2015-05-23   8267  Sat 2015     5         143           23           7
144 2015-05-24   7261  Sun 2015     5         144           24           1
145 2015-05-25   7746  Mon 2015     5         145           25           2
146 2015-05-26  12242  Tue 2015     5         146           26           3
147 2015-05-27  12845  Wed 2015     5         147           27           4
148 2015-05-28  12722  Thu 2015     5         148           28           5
149 2015-05-29  12168  Fri 2015     5         149           29           6
150 2015-05-30   8431  Sat 2015     5         150           30           7
151 2015-05-31   7367  Sun 2015     5         151           31           1
152 2015-06-01  11793  Mon 2015     6         152            1           2
153 2015-06-02  11994  Tue 2015     6         153            2           3
154 2015-06-03  11890  Wed 2015     6         154            3           4
155 2015-06-04  11918  Thu 2015     6         155            4           5
156 2015-06-05  12303  Fri 2015     6         156            5           6
157 2015-06-06   8313  Sat 2015     6         157            6           7
158 2015-06-07   7230  Sun 2015     6         158            7           1
159 2015-06-08  11938  Mon 2015     6         159            8           2
160 2015-06-09  12479  Tue 2015     6         160            9           3
161 2015-06-10  12123  Wed 2015     6         161           10           4
162 2015-06-11  12029  Thu 2015     6         162           11           5
163 2015-06-12  12064  Fri 2015     6         163           12           6
164 2015-06-13   8104  Sat 2015     6         164           13           7
165 2015-06-14   7292  Sun 2015     6         165           14           1
166 2015-06-15  12348  Mon 2015     6         166           15           2
167 2015-06-16  12400  Tue 2015     6         167           16           3
168 2015-06-17  12192  Wed 2015     6         168           17           4
169 2015-06-18  12323  Thu 2015     6         169           18           5
170 2015-06-19  11767  Fri 2015     6         170           19           6
171 2015-06-20   8451  Sat 2015     6         171           20           7
172 2015-06-21   7580  Sun 2015     6         172           21           1
173 2015-06-22  11883  Mon 2015     6         173           22           2
174 2015-06-23  12351  Tue 2015     6         174           23           3
175 2015-06-24  12292  Wed 2015     6         175           24           4
176 2015-06-25  12416  Thu 2015     6         176           25           5
177 2015-06-26  12152  Fri 2015     6         177           26           6
178 2015-06-27   8652  Sat 2015     6         178           27           7
179 2015-06-28   7371  Sun 2015     6         179           28           1
180 2015-06-29  11961  Mon 2015     6         180           29           2
181 2015-06-30  12932  Tue 2015     6         181           30           3
182 2015-07-01  13033  Wed 2015     7         182            1           4
183 2015-07-02  13038  Thu 2015     7         183            2           5
184 2015-07-03  10375  Fri 2015     7         184            3           6
185 2015-07-04   7944  Sat 2015     7         185            4           7
186 2015-07-05   7164  Sun 2015     7         186            5           1
187 2015-07-06  12088  Mon 2015     7         187            6           2
188 2015-07-07  13426  Tue 2015     7         188            7           3
189 2015-07-08  12788  Wed 2015     7         189            8           4
190 2015-07-09  12517  Thu 2015     7         190            9           5
191 2015-07-10  12396  Fri 2015     7         191           10           6
192 2015-07-11   8769  Sat 2015     7         192           11           7
193 2015-07-12   7591  Sun 2015     7         193           12           1
194 2015-07-13  11835  Mon 2015     7         194           13           2
195 2015-07-14  12862  Tue 2015     7         195           14           3
196 2015-07-15  12853  Wed 2015     7         196           15           4
197 2015-07-16  12502  Thu 2015     7         197           16           5
198 2015-07-17  12291  Fri 2015     7         198           17           6
199 2015-07-18   8546  Sat 2015     7         199           18           7
200 2015-07-19   7681  Sun 2015     7         200           19           1
201 2015-07-20  12424  Mon 2015     7         201           20           2
202 2015-07-21  13264  Tue 2015     7         202           21           3
203 2015-07-22  12519  Wed 2015     7         203           22           4
204 2015-07-23  12852  Thu 2015     7         204           23           5
205 2015-07-24  12612  Fri 2015     7         205           24           6
206 2015-07-25   8748  Sat 2015     7         206           25           7
207 2015-07-26   7836  Sun 2015     7         207           26           1
208 2015-07-27  12416  Mon 2015     7         208           27           2
209 2015-07-28  13240  Tue 2015     7         209           28           3
210 2015-07-29  12739  Wed 2015     7         210           29           4
211 2015-07-30  12797  Thu 2015     7         211           30           5
212 2015-07-31  12269  Fri 2015     7         212           31           6
213 2015-08-01   8851  Sat 2015     8         213            1           7
214 2015-08-02   7907  Sun 2015     8         214            2           1
215 2015-08-03  12449  Mon 2015     8         215            3           2
216 2015-08-04  13149  Tue 2015     8         216            4           3
217 2015-08-05  12793  Wed 2015     8         217            5           4
218 2015-08-06  12872  Thu 2015     8         218            6           5
219 2015-08-07  12770  Fri 2015     8         219            7           6
220 2015-08-08   8956  Sat 2015     8         220            8           7
221 2015-08-09   7726  Sun 2015     8         221            9           1
222 2015-08-10  12333  Mon 2015     8         222           10           2
223 2015-08-11  13010  Tue 2015     8         223           11           3
224 2015-08-12  12960  Wed 2015     8         224           12           4
225 2015-08-13  12591  Thu 2015     8         225           13           5
226 2015-08-14  12861  Fri 2015     8         226           14           6
227 2015-08-15   8938  Sat 2015     8         227           15           7
228 2015-08-16   7896  Sun 2015     8         228           16           1
229 2015-08-17  12337  Mon 2015     8         229           17           2
230 2015-08-18  13197  Tue 2015     8         230           18           3
231 2015-08-19  12966  Wed 2015     8         231           19           4
232 2015-08-20  13002  Thu 2015     8         232           20           5
233 2015-08-21  12834  Fri 2015     8         233           21           6
234 2015-08-22   9153  Sat 2015     8         234           22           7
235 2015-08-23   7821  Sun 2015     8         235           23           1
236 2015-08-24  12274  Mon 2015     8         236           24           2
237 2015-08-25  13149  Tue 2015     8         237           25           3
238 2015-08-26  12729  Wed 2015     8         238           26           4
239 2015-08-27  12829  Thu 2015     8         239           27           5
240 2015-08-28  12719  Fri 2015     8         240           28           6
241 2015-08-29   8772  Sat 2015     8         241           29           7
242 2015-08-30   7805  Sun 2015     8         242           30           1
243 2015-08-31  12142  Mon 2015     8         243           31           2
244 2015-09-01  13231  Tue 2015     9         244            1           3
245 2015-09-02  12799  Wed 2015     9         245            2           4
246 2015-09-03  12839  Thu 2015     9         246            3           5
247 2015-09-04  12862  Fri 2015     9         247            4           6
248 2015-09-05   8950  Sat 2015     9         248            5           7
249 2015-09-06   7778  Sun 2015     9         249            6           1
250 2015-09-07   8127  Mon 2015     9         250            7           2
251 2015-09-08  12823  Tue 2015     9         251            8           3
252 2015-09-09  13949  Wed 2015     9         252            9           4
253 2015-09-10  13925  Thu 2015     9         253           10           5
254 2015-09-11  12295  Fri 2015     9         254           11           6
255 2015-09-12   9062  Sat 2015     9         255           12           7
256 2015-09-13   7900  Sun 2015     9         256           13           1
257 2015-09-14  12699  Mon 2015     9         257           14           2
258 2015-09-15  13439  Tue 2015     9         258           15           3
259 2015-09-16  13136  Wed 2015     9         259           16           4
260 2015-09-17  13319  Thu 2015     9         260           17           5
261 2015-09-18  13229  Fri 2015     9         261           18           6
262 2015-09-19   9195  Sat 2015     9         262           19           7
263 2015-09-20   8078  Sun 2015     9         263           20           1
264 2015-09-21  12567  Mon 2015     9         264           21           2
265 2015-09-22  13047  Tue 2015     9         265           22           3
266 2015-09-23  12595  Wed 2015     9         266           23           4
267 2015-09-24  12942  Thu 2015     9         267           24           5
268 2015-09-25  12792  Fri 2015     9         268           25           6
269 2015-09-26   8927  Sat 2015     9         269           26           7
270 2015-09-27   7778  Sun 2015     9         270           27           1
271 2015-09-28  12158  Mon 2015     9         271           28           2
272 2015-09-29  12791  Tue 2015     9         272           29           3
273 2015-09-30  12284  Wed 2015     9         273           30           4
274 2015-10-01  13043  Thu 2015    10         274            1           5
275 2015-10-02  12399  Fri 2015    10         275            2           6
276 2015-10-03   8579  Sat 2015    10         276            3           7
277 2015-10-04   7530  Sun 2015    10         277            4           1
278 2015-10-05  12166  Mon 2015    10         278            5           2
279 2015-10-06  12520  Tue 2015    10         279            6           3
280 2015-10-07  12429  Wed 2015    10         280            7           4
281 2015-10-08  12458  Thu 2015    10         281            8           5
282 2015-10-09  12282  Fri 2015    10         282            9           6
283 2015-10-10   8550  Sat 2015    10         283           10           7
284 2015-10-11   7571  Sun 2015    10         284           11           1
285 2015-10-12  11633  Mon 2015    10         285           12           2
286 2015-10-13  12094  Tue 2015    10         286           13           3
287 2015-10-14  12205  Wed 2015    10         287           14           4
288 2015-10-15  12522  Thu 2015    10         288           15           5
289 2015-10-16  11917  Fri 2015    10         289           16           6
290 2015-10-17   8233  Sat 2015    10         290           17           7
291 2015-10-18   7211  Sun 2015    10         291           18           1
292 2015-10-19  11388  Mon 2015    10         292           19           2
293 2015-10-20  12500  Tue 2015    10         293           20           3
294 2015-10-21  12093  Wed 2015    10         294           21           4
295 2015-10-22  12013  Thu 2015    10         295           22           5
296 2015-10-23  11942  Fri 2015    10         296           23           6
297 2015-10-24   8239  Sat 2015    10         297           24           7
298 2015-10-25   7288  Sun 2015    10         298           25           1
299 2015-10-26  11923  Mon 2015    10         299           26           2
300 2015-10-27  12485  Tue 2015    10         300           27           3
301 2015-10-28  12266  Wed 2015    10         301           28           4
302 2015-10-29  11950  Thu 2015    10         302           29           5
303 2015-10-30  11529  Fri 2015    10         303           30           6
304 2015-10-31   8049  Sat 2015    10         304           31           7
305 2015-11-01   7731  Sun 2015    11         305            1           1
306 2015-11-02  12236  Mon 2015    11         306            2           2
307 2015-11-03  12524  Tue 2015    11         307            3           3
308 2015-11-04  12279  Wed 2015    11         308            4           4
309 2015-11-05  12452  Thu 2015    11         309            5           5
310 2015-11-06  11909  Fri 2015    11         310            6           6
311 2015-11-07   8517  Sat 2015    11         311            7           7
312 2015-11-08   7324  Sun 2015    11         312            8           1
313 2015-11-09  11688  Mon 2015    11         313            9           2
314 2015-11-10  12451  Tue 2015    11         314           10           3
315 2015-11-11  12311  Wed 2015    11         315           11           4
316 2015-11-12  12329  Thu 2015    11         316           12           5
317 2015-11-13  11643  Fri 2015    11         317           13           6
318 2015-11-14   8221  Sat 2015    11         318           14           7
319 2015-11-15   7378  Sun 2015    11         319           15           1
320 2015-11-16  11791  Mon 2015    11         320           16           2
321 2015-11-17  12321  Tue 2015    11         321           17           3
322 2015-11-18  12230  Wed 2015    11         322           18           4
323 2015-11-19  12504  Thu 2015    11         323           19           5
324 2015-11-20  12495  Fri 2015    11         324           20           6
325 2015-11-21   8392  Sat 2015    11         325           21           7
326 2015-11-22   7427  Sun 2015    11         326           22           1
327 2015-11-23  12419  Mon 2015    11         327           23           2
328 2015-11-24  13051  Tue 2015    11         328           24           3
329 2015-11-25  11745  Wed 2015    11         329           25           4
330 2015-11-26   7332  Thu 2015    11         330           26           5
331 2015-11-27   8942  Fri 2015    11         331           27           6
332 2015-11-28   7734  Sat 2015    11         332           28           7
333 2015-11-29   7230  Sun 2015    11         333           29           1
334 2015-11-30  12214  Mon 2015    11         334           30           2
335 2015-12-01  13185  Tue 2015    12         335            1           3
336 2015-12-02  12708  Wed 2015    12         336            2           4
337 2015-12-03  12281  Thu 2015    12         337            3           5
338 2015-12-04  11828  Fri 2015    12         338            4           6
339 2015-12-05   8262  Sat 2015    12         339            5           7
340 2015-12-06   7194  Sun 2015    12         340            6           1
341 2015-12-07  11567  Mon 2015    12         341            7           2
342 2015-12-08  12328  Tue 2015    12         342            8           3
343 2015-12-09  11857  Wed 2015    12         343            9           4
344 2015-12-10  12259  Thu 2015    12         344           10           5
345 2015-12-11  11406  Fri 2015    12         345           11           6
346 2015-12-12   8073  Sat 2015    12         346           12           7
347 2015-12-13   7262  Sun 2015    12         347           13           1
348 2015-12-14  11753  Mon 2015    12         348           14           2
349 2015-12-15  12437  Tue 2015    12         349           15           3
350 2015-12-16  12282  Wed 2015    12         350           16           4
351 2015-12-17  12339  Thu 2015    12         351           17           5
352 2015-12-18  12247  Fri 2015    12         352           18           6
353 2015-12-19   8368  Sat 2015    12         353           19           7
354 2015-12-20   7361  Sun 2015    12         354           20           1
355 2015-12-21  12692  Mon 2015    12         355           21           2
356 2015-12-22  13131  Tue 2015    12         356           22           3
357 2015-12-23  11595  Wed 2015    12         357           23           4
358 2015-12-24   8714  Thu 2015    12         358           24           5
359 2015-12-25   6515  Fri 2015    12         359           25           6
360 2015-12-26   7294  Sat 2015    12         360           26           7
361 2015-12-27   7518  Sun 2015    12         361           27           1
362 2015-12-28  13100  Mon 2015    12         362           28           2
363 2015-12-29  13458  Tue 2015    12         363           29           3
364 2015-12-30  13100  Wed 2015    12         364           30           4
365 2015-12-31  11608  Thu 2015    12         365           31           5

Obtained from the National Center for Health Statistics, National Vital Statistics System, Natality, 2015 data.

How do we make this plot?

Two Questions:

  1. What do we want R to do? (What is the goal?)

  2. What does R need to know?

How do we make this plot?

  1. Goal: scatterplot = a plot with points

  2. What does R need to know?

    • data source: Births2015

    • aesthetics:

      • date -> x
      • births -> y
    • points

How do we make this plot?

ggplot(data = Births2015, 
       aes(x = date, y = births)) + 
  geom_point() +
  labs(title = "US Births in 2015")

ggplot() +
  geom_point(data = Births2015, 
             aes(x = date, y = births)) +
  labs(title = "US Births in 2015")

Layers: layer 0

ggplot() 

Layers: layer 1

ggplot(data = Births2015, 
       aes(x = date, y = births)) 

Layers: layer 2

ggplot(data = Births2015, 
       aes(x = date, y = births)) + 
  geom_point()

Layers: layer 3

ggplot(data = Births2015, 
       aes(x = date, y = births)) + 
  geom_point() +
  labs(title = "US Births in 2015")

How do we make this plot?

What has changed?

  • new aesthetic: mapping color to day of week

How do we make this plot?

ggplot(data = Births2015,
       aes(x = date,
           y = births, 
           color = wday)) +
  geom_point() +
  labs(title = "US Births in 2015")

How do we make this plot?

How do we make this plot?

lines instead of dots!

ggplot(data = Births2015,
         aes(x = date, 
             y = births,
             color = wday)) +
  geom_line() +
  labs(title = "US Births in 2015")

How do we make this plot?

How do we make this plot?

Now there are two layers: one with points and one with lines

ggplot(data = Births2015,
       aes(x = date,
           y = births,
           color = wday)) + 
  geom_point() +  
  geom_line() +
  labs(title = "US Births in 2015")
  • The layers are placed one on top of the other: the points are below and the lines are above.

  • data and aes specified in ggplot() affect all geoms

What does this code do?

ggplot(data = Births2015,
       aes(x = date, y = births, color = "navy")) + 
  geom_point() +
  labs(title = "US Births in 2015") 

What does this code do?

ggplot(data = Births2015,
       aes(x = date, y = births, color = "navy")) + 
  geom_point()  +
  labs(title = "US Births in 2015")

This is mapping the color aesthetic to a new variable with only one value (“navy”).
So all the dots get set to the same color, but it’s not navy.

Setting vs. Mapping

If we want to set the color to be navy for all of the dots, we do it outside the aes() designation:

ggplot(data = Births2015,
       aes(x = date, y = births)) +   # map variables 
  geom_point(color = "navy")    +   # set attributes
  labs(title = "US Births in 2015")
  • Note that color = "navy" is now outside of the aesthetics list. That’s how ggplot2 distinguishes between mapping and setting.

How do we make this plot?

How do we make this plot?

ggplot(data = Births2015,
       aes(x = date,
           y = births)) + 
  geom_line(aes(color = wday)) +      
  geom_point(color = "navy")  +         
  labs(title = "US Births in 2015")
  • ggplot() establishes the default data and aesthetics for the geoms, but each geom may change these defaults.

  • good practice: put into ggplot() the things that affect all (or most) of the layers; rest in geom_XXXX()

Setting vs. Mapping (again)

Information gets passed to the plot via:

  1. map the variable information inside the aes (aesthetic) command

  2. set the non-variable information outside the aes (aesthetic) command

geom_*() types

apropos("^geom_")
 [1] "geom_abline"            "geom_area"              "geom_ash"              
 [4] "geom_bar"               "geom_bin_2d"            "geom_bin2d"            
 [7] "geom_blank"             "geom_boxplot"           "geom_col"              
[10] "geom_contour"           "geom_contour_filled"    "geom_count"            
[13] "geom_crossbar"          "geom_curve"             "geom_density"          
[16] "geom_density_2d"        "geom_density_2d_filled" "geom_density2d"        
[19] "geom_density2d_filled"  "geom_dotplot"           "geom_errorbar"         
[22] "geom_errorbarh"         "geom_freqpoly"          "geom_function"         
[25] "geom_hex"               "geom_histogram"         "geom_hline"            
[28] "geom_jitter"            "geom_label"             "geom_line"             
[31] "geom_linerange"         "geom_lm"                "geom_map"              
[34] "geom_path"              "geom_point"             "geom_pointrange"       
[37] "geom_polygon"           "geom_qq"                "geom_qq_line"          
[40] "geom_quantile"          "geom_rangeframe"        "geom_raster"           
[43] "geom_rect"              "geom_ribbon"            "geom_rug"              
[46] "geom_segment"           "geom_sf"                "geom_sf_label"         
[49] "geom_sf_text"           "geom_smooth"            "geom_spline"           
[52] "geom_spoke"             "geom_step"              "geom_text"             
[55] "geom_tile"              "geom_tufteboxplot"      "geom_violin"           
[58] "geom_vline"            

geom_*() pics

https://ggplot2.tidyverse.org/

geom_*() help

help pages will tell you their aesthetics, default stats, etc.

?geom_area             # for example

Let’s try geom_area

ggplot(data = Births2015,
       aes(x = date,
           y = births, 
           fill = wday)) + 
  geom_area() +
  labs(title = "US Births in 2015")

Let’s try geom_area

ggplot(data = Births2015,
       aes(x = date, y = births, fill = wday)) + 
  geom_area() +
  labs(title = "US Births in 2015")

… not a good plot

  • overplotting is hiding much of the data
  • extending y-axis to 0 may or may not be desirable.

Side note: what makes a plot good?

Most (all?) graphics are intended to help us make comparisons

  • How does something change over time?
  • Do my treatments matter? How much?
  • Do treatment and control respond the same way?

Key plot metric

Does my plot make the comparisons I am interested in:

  • easily, and
  • accurately?

Common geom() functions

  • geom_point(): scatter plot (x vs y)
  • geom_bar(): bar charts (categorical data)
  • geom_col(): bar chart with height from y variable (categorical data)
  • geom_histogram(): distribution of numeric variable
  • geom_line(): connect the dots across (x,y) coordinates

Time for some different data

HELPrct: Health Evaluation and Linkage to Primary care randomized clinical trial. Subjects admitted for treatment for addiction to one of three substances.

head(HELPrct)
age anysubstatus anysub cesd d1 daysanysub dayslink drugrisk e2b female sex g1b homeless i1 i2 id indtot linkstatus link mcs pcs pss_fr racegrp satreat sexrisk substance treat avg_drinks max_drinks hospitalizations
37 1 yes 49 3 177 225 0 NA 0 male yes housed 13 26 1 39 1 yes 25.111990 58.41369 0 black no 4 cocaine yes 13 26 3
37 1 yes 30 22 2 NA 0 NA 0 male yes homeless 56 62 2 43 NA NA 26.670307 36.03694 1 white no 7 alcohol yes 56 62 22
26 1 yes 39 0 3 365 20 NA 0 male no housed 0 0 3 41 0 no 6.762923 74.80633 13 black no 2 heroin no 0 0 0
39 1 yes 15 2 189 343 0 1 1 female no housed 5 5 4 28 0 no 43.967880 61.93168 11 white yes 4 heroin no 5 5 2
32 1 yes 39 12 2 57 0 1 0 male no homeless 10 13 5 38 1 yes 21.675755 37.34558 10 black no 6 cocaine no 10 13 12
47 1 yes 6 1 31 365 0 NA 1 female no housed 4 4 6 29 0 no 55.508991 46.47521 5 black no 5 cocaine yes 4 4 1

Who are the people in the study?

ggplot(data = HELP_data,
       aes(x = substance)) + 
  geom_bar() +
  labs(title = "HELP trial")
  • Hmm. What’s up with y?

Who are the people in the study?

ggplot(data = HELP_data,
       aes(x = substance)) + 
  geom_bar() +
  labs(title = "HELP trial")
  • Hmm. What’s up with y?

    • stat_bin() is being applied to the data before the geom_bar() gets to do its thing. Binning creates the y values.

Who are the people in the study?

ggplot(data = HELP_data,
       aes(x = substance,
           fill = children)) + 
  geom_bar() +
  labs(title = "HELP trial")

Who are the people in the study?

ggplot(HELP_data,
       aes(x = substance,
           fill = children)) + 
  geom_bar(position = "fill") +
  labs(title = "HELP trial",
       y = "actually, percent")

How old are people in the HELP study?

`stat_bin()` using `bins = 30`. Pick better value `binwidth`.

ggplot(data = HELP_data,
       aes(x = age)) + 
  geom_histogram() +
  labs(title = "HELP trial")

Notice the messages

  • stat_bin: Histograms are not mapping the raw data but binned data.
    stat_bin() performs the data transformation.

  • binwidth: a default binwidth has been selected, but we should really choose our own.

Setting the binwidth manually

ggplot(data = HELP_data,
       aes(x = age)) + 
  geom_histogram(binwidth = 2) +
  labs(title = "HELP trial")

How old are people in the HELP study? – Other geoms

ggplot(data = HELP_data,
       aes(x = age)) + 
  geom_freqpoly(binwidth = 2) +
  labs(title = "HELP clinical trial at detoxification unit")

ggplot(data = HELP_data,
       aes(x = age)) + 
  geom_density() +
  labs(title = "HELP clinical trial at detoxification unit")

Selecting stat and geom manually

Every geom comes with a default stat

  • for simple cases, the stat is stat_identity() which does nothing
  • we can mix and match geoms and stats however we like
ggplot(data = HELP_data,
       aes(x = age)) + 
  geom_line(stat = "density") +
  labs(title = "HELP clinical trial at detoxification unit")

Selecting stat and geom manually

Every stat comes with a default geom, every geom with a default stat

  • we can specify stats instead of geom, if we prefer
  • we can mix and match geoms and stats however we like
ggplot(data = HELP_data,
       aes(x = age)) + 
  stat_density(geom = "line") +
  labs(title = "HELP clinical trial at detoxification unit")

More combinations

ggplot(data = HELP_data,
       aes(x = age)) + 
  geom_point(stat = "bin", binwidth = 3) + 
  geom_line(stat = "bin", binwidth = 3)  +
  labs(title = "HELP clinical trial at detoxification unit")

More combinations

ggplot(data = HELP_data,
       aes(x = age)) + 
  geom_area(stat = "bin", binwidth = 3)  +
  labs(title = "HELP clinical trial at detoxification unit")

More combinations

ggplot(data = HELP_data,
       aes(x = age)) + 
  geom_point(stat = "bin", 
             binwidth = 3, 
             aes(size = ..count..)) +
  geom_line(stat = "bin", binwidth = 3) +
  labs(title = "HELP clinical trial at detoxification unit")

How much drinking? (i1)

HELP_data |> 
  ggplot(aes(x = i1)) + geom_histogram() +
  labs(title = "HELP clinical trial at detoxification unit")

How much drinking? (i1)

HELP_data |> 
  ggplot(aes(x = i1)) + geom_density() +
  labs(title = "HELP clinical trial at detoxification unit")

How much drinking? (i1)

HELP_data |> 
  ggplot(aes(x = i1)) + geom_area(stat = "density") +
  labs(title = "HELP clinical trial at detoxification unit")

Covariates: Adding in more variables

Using color and linetype:

ggplot(data = HELP_data,
       aes(x = i1,
           color = substance,
           linetype = children)) + 
  geom_line(stat = "density") +
  labs(title = "HELP clinical trial at detoxification unit")

Using color and facets

ggplot(data = HELP_data,
       aes(x = i1, color = substance)) + 
  geom_line(stat = "density") + 
  facet_grid( . ~ children ) +
  labs(title = "HELP clinical trial at detoxification unit")

ggplot(data = HELP_data,
       aes(x = i1, color = substance)) + 
  geom_line(stat = "density") + 
  facet_grid( children ~ . ) +
  labs(title = "HELP clinical trial at detoxification unit")

Boxplots

Boxplots use stat_quantile() (five number summary).

The quantitative variable must be y, and there must be an additional x variable.

HELP_data |> 
  ggplot(aes(x = substance, y = age, color = children)) + 
  geom_boxplot() +
  labs(title = "HELP clinical trial at detoxification unit")

Horizontal boxplots

Horizontal boxplots are obtained by flipping the coordinate system:

  • coord_flip() may be used with other plots as well to reverse the roles of x and y on the plot.
ggplot(data = HELP_data,
       aes(x = substance, 
           y = age, 
           color = children)) + 
  geom_boxplot() +
  coord_flip() +
  labs(title = "HELP clinical trial at detoxification unit")

Axes scaling with boxplots

We can scale the continuous axis

ggplot(data = HELP_data,
       aes(x = substance, 
           y = age, 
           color = children)) + 
  geom_boxplot() +
  coord_trans(y = "exp") +
  labs(title = "HELP clinical trial at detoxification unit")

Give me some space

We’ve triggered a new feature: dodge (for dodging things left/right). We can control how much if we set the dodge manually.

ggplot(data = HELP_data,
       aes(x = substance, 
           y = age, 
           color = children)) + 
  geom_boxplot(position = position_dodge(width=1)) +
  labs(title = "HELP clinical trial at detoxification unit")

Issues with bigger data

  • Although we can see a generally positive association (as we would expect), the overplotting may be hiding information.
library(NHANES)
dim(NHANES)
[1] 10000    76
ggplot(data = NHANES,
       aes(x = Height, y = Weight)) +
  geom_point() + 
  facet_grid( Gender ~ PregnantNow )

Using alpha (opacity)

One way to deal with overplotting is to set the opacity low.

ggplot(data = NHANES,
       aes(x = Height, y = Weight)) +
  geom_point(alpha=0.01) + 
  facet_grid( Gender ~ PregnantNow )

geom_density2d

Alternatively (or simultaneously) we might prefer a different geom altogether.

ggplot(data = NHANES,
       aes(x = Height, y = Weight)) +
  geom_density2d() + 
  facet_grid( Gender ~ PregnantNow )

Multiple layers

ggplot(data = HELP_data, 
       aes(x = children, y = age)) +
  geom_boxplot(outlier.size = 0) +
  geom_point(alpha=.6) +
  coord_flip() +
  labs(title = "HELP clinical trial at detoxification unit")

ggplot(data = HELP_data,
       aes(x = children, y = age)) +
  geom_boxplot(outlier.size = 0) +
  geom_jitter(alpha=.6, width = 0.1) +
  coord_flip() +
  labs(title = "HELP clinical trial at detoxification unit")

Multiple layers

ggplot(data = HELP_data,
       aes(x = children, y = age)) +
  geom_boxplot(outlier.size = 0) +
  geom_point(alpha=.6, 
             position = position_jitter(width=.1, height=0)) +
  coord_flip() +
  labs(title = "HELP clinical trial at detoxification unit")

Things I haven’t mentioned (much)

  • coords (coord_flip() is good to know about)

  • themes (for customizing appearance)

  • position (position_dodge(), position_jitterdodge(), position_stack(), etc.)

  • transforming axes

themes

library(ggthemes)
ggplot(Births2015, aes(x = date, y = births)) + 
  geom_point() + 
  theme_wsj()

jitterdodge()

ggplot(data = HELP_data, 
       aes(x = substance, y = age, color = children)) +
  geom_boxplot(coef = 10, position = position_dodge()) +
  geom_point(aes(color = children, 
                 fill = children), 
             position = position_jitterdodge()) +
  labs(title = "HELP clinical trial at detoxification unit")

A little bit of everything

ggplot(data = HELP_data, aes(x = substance, y = age, color = children)) +
  geom_boxplot(coef = 10, position = position_dodge(width=1)) +
  geom_point(aes(fill = children), alpha=.5, 
             position = position_jitterdodge(dodge.width=1, jitter.width = 0.2)) + 
  facet_wrap(~homeless) +
  labs(title = "HELP clinical trial at detoxification unit")

Want to learn more?